package jmine.tec.web.wicket.pages.main;

import java.io.PrintWriter;
import java.io.StringWriter;

import jmine.tec.web.wicket.pages.Template;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.HiddenField;
import org.apache.wicket.model.Model;

import bancosys.tec.exception.MessageCreator;

/**
 * Página de erro.
 */
public class Error extends Template {

    /**
     * @param t exceção.
     */
    public Error(Throwable t) {
        this(t.getMessage(), t);
    }

    /**
     * @param message mensagem.
     * @param t exceção.
     */
    public Error(String message, Throwable t) {
        super.add(new Label("message", message != null ? message : t.getMessage()));
        super.add(new HiddenField<String>("stack", new Model<String>(this.stackTrace(t))));
    }

    /**
     * @param t exceção.
     * @return {@link String} com a stackTrace.
     */
    private String stackTrace(Throwable t) {
        StringWriter strWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(strWriter);
        t.printStackTrace(printWriter);
        printWriter.close();
        return strWriter.toString();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected MessageCreator getHelpTextCreator() {
        // Não possui mensagem descritiva de "ajuda"
        return null;
    }
}
